<?php

class BugController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	
    	// in my house
    }

    public function indexAction()
    {
        // action body        
    }

    public function bugAction()
    {
        // action body     
    }

    public function submitAction()
    {
        // action body
        $bugReportForm = new Form_BugReportForm();
        $bugReportForm->setAction('/bug/submit');
        $bugReportForm->setMethod('post');
        
        if($this->getRequest()->isPost()) {
        	if($bugReportForm->isValid($_POST)) {
        		$bugModel = new Application_Model_Bug();        		
        		// if the form is valid then create the new bug
        		$result = $bugModel->createBug(
        			$bugReportForm->getValue('author'),
        			$bugReportForm->getValue('email'),        				
        			$bugReportForm->getValue('date') ,
        			$bugReportForm->getValue('url'),
        			$bugReportForm->getValue('description'),
        			$bugReportForm->getValue('priority'),
        			$bugReportForm->getValue('status')       				
        		);      	

        		if($result) {
        			$this->_forward('confirm');
        		}   
        	}
        }
        $this->view->form = 	$bugReportForm;
    }

    public function confirmAction()
    {
        // action body        
    }

    public function listAction()
    {
    	// get the filter form
   	 	$listToolsForm = new Application_Form_BugReportListToolsForm();
   	 	$listToolsForm->setAction('/bug/list');
       	$listToolsForm->setMethod('post');
       	$this->view->listToolsForm = $listToolsForm;
       	
       	// set sort and filter criteria. you need
       	$sort = $this->_request->getParam('sort', null);
       	$filterField  =  $this->_request->getParam('filter_field', null);
       	$filterValue =  $this->_request->getParam('filter', null);
       	
       	if(!empty($filterField)) {
   			$filter[$filterField] = $filterValue;
       	} else {
       		$filter = null;
       	}
       	// now you need to manually set there controls vslues
   	   	$listToolsForm->getElement('sort')->setValue($sort);
   	   	$listToolsForm->getElement('filter_field')->setValue($filterField);
   	   	$listToolsForm->getElement('filter')->setValue($filterValue);
   	 	// fetch the bug paginator adapter
   	   	$bugModels = new Application_Model_Bug();
   	   	$adapter = $bugModels->fetchPaginatorAdapter($filter, $sort);
   	   	$paginator = new Zend_Paginator($adapter);
   	   	// show 10 bugs per page
   	   	$paginator->setItemCountPerPage(10);
   	   	// if none is set then default to page 1.
   	   	$page = $this->_request->getParam('page', 1);
   	   	$paginator->setCurrentPageNumber($page);
   	   	// pass paginator to the view to render
   	   	$this->view->paginator = $paginator;
    }
       	 
    public function editAction()
    {
        // action body
        $bugModel = new Application_Model_Bug();
        $bugReportForm = new Form_BugReportForm();
        $bugReportForm->setAction('/bug/edit');
        $bugReportForm->setMethod('post');

        $id = $this->_request->getParam('id');
        $bug = $bugModel->find($id)->current();
        $bugReportForm->populate($bug->toArray());
        // format the date field
        $bugReportForm->getElement('date')->setValue(date('m-d-y', $bug->date));
        $this->view->bug = $bug;
        $this->view->form = $bugReportForm;        
    }

}











